/**
 * @Title: settleAnalyseMain_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月26日 上午10:32:01
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('SettleAnalyseDepartCtrl',
	    ['$scope','SettleAnalyseDepartService','$stateParams', '$filter',
	     	'SettleAnalyseChartService','SettleAnalyseService','Modal','BaseURL','CommunityService',
	     function($scope,SettleAnalyseDepartService,$stateParams,$filter,
	    		 SettleAnalyseChartService,SettleAnalyseService,Modal,baseURL,CommunityService){
		
	    	/*********************************************************
			 * 
			 * Scope & Variables Init
			 * 
			 */
	    	
	    	var departId = $stateParams.id,
	    		checkedArray = [],
	    		doctor = $stateParams.doctor,
	    		orgCode = $stateParams.orgCode,
	    		basicTableData = [];
	    	
	    	$scope.page = {
    			limit:10	
	    	};
	    	
	    	SettleAnalyseDepartService.deptId = departId;
	    	
	    	/*********************************************************
	    	 * 
	    	 * 
	    	 * 
			 * 
			 * private function
			 * 
			 */
	    	//查询
	    	function query(params) {
	    		params = $.extend({
					id:departId,
					indicator:$scope.$parent.query.indicator,
					startDate:$scope.$parent.query.date.start,
					endDate:$scope.$parent.query.date.end,
					doctor:$scope.$parent.query.doctor,
					orgId:orgCode,
					start:1,
					limit:$scope.page.limit
				},params);
	    		SettleAnalyseDepartService.listDoctors(params).
					success(function(res){
						angular.forEach(basicTableData,function(data,index){
							data.checked = false;
						});
						if (res.data) {
							var arr = basicTableData.concat(toFixedDatas(res.data));
							
							SettleAnalyseService.checkedInit(arr);
							
							$scope.tableData = arr;
							$scope.page.total = res.total;
						}
						else {
							$scope.page.total = 0;
							$scope.tableData = [];
						}
					});
	    	}
	    	
	    	
	    	
	    	// 导出
			$scope.onExport=function(){
				//判断日期跨度不能大于一年
				var checkTemp = CommunityService.checkDateSpan($scope.$parent.query.date.start,$scope.$parent.query.date.end);
				if(checkTemp!=''&& checkTemp.length>0){
					Modal.alert(checkTemp);
					return false;
				}
				var date=$scope.$parent.query.date;
				var startDate=date.start;
				var endDate=date.end;
				var indicator=$scope.$parent.query.indicator;
				var url=baseURL+"indication/doctorExport.do" +
						"?indicator="+indicator+"&dataType="+SettleAnalyseService.typeSelected+
						"&startDate="+startDate+"&endDate="+endDate+"&id="+departId+'&orgId='+orgCode;
			    window.location.href = url;
			}
	    	
	    	
	    	/*********************************************************
			 * 
			 * Event Handler
			 * 
			 */
	    	
	    	$scope.onRowChecked = function(data) {
	    		checkedArray = SettleAnalyseService.checkedArray;
	    		if(data.checked && checkedArray.length >= 2) {
	    			Modal.alert('勾选超过2个!');
	    			data.checked = false;
	    			return;
	    		}
	    		var index = checkedArray.indexOf(data);
	    		if(data.checked && index == -1) {
	    			checkedArray.push(data);
	    		} else {
	    			if(index != -1) {
	    				checkedArray.splice(index,1);
	    			}
	    		}
	    		$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',checkedArray);
	    	}
	    	
	    	$scope.$on('pageTool.skipPage', function(e,page){
	    		query({
	    			start:page
	    		});
			});
	    	
	    	//重置
			$scope.$on('settleAnalyse.searchReset', function(){
	    		$scope.$parent.query.doctor = '';
	    	});
			
	    	$scope.$on('settleAnalyse.searchQuery', function() {
	    		$scope.page.current = 1;
	    		query({
	    			start:1
	    		});
	    		getDeptTableBasicData();
	    		$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',SettleAnalyseService.checkedArray);
	    	});
			
//			$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
//				$scope.page.current = 1;
//	    		query({
//	    			start:1
//	    		});
//			});
	    	
	    	
	    	/*********************************************************
			 * 
			 * Init
			 * 
			 */
			
			//以万为单位
			function toFixedDatas(datas) {
				var newDatas = [];
				if ($scope.$parent
					&& $scope.$parent.query 
					&& $scope.$parent.query.indicator ) {
						var isFlag = false;
						angular.forEach($scope.$parent.query.indicators,function(indicator,index){
							if ($scope.$parent.query.indicator == indicator) {
								isFlag = true;
							}
						});
						newDatas = toNewDatas(datas,isFlag);
					}
				return newDatas;
			}
			
			function toNewDatas(datas,isFlag) {
				angular.forEach(datas,function(data,index){
					if (isFlag) {
						data.first = (parseFloat(data.first)/10000).toFixed(2);
						data.second = (parseFloat(data.second)/10000).toFixed(2);
						data.third = (parseFloat(data.third)/10000).toFixed(2);
						data.fourth = (parseFloat(data.fourth)/10000).toFixed(2);
						data.fifth = (parseFloat(data.fifth)/10000).toFixed(2);
						data.sixth = (parseFloat(data.sixth)/10000).toFixed(2);
						data.seventh = (parseFloat(data.seventh)/10000).toFixed(2);
						data.eighth = (parseFloat(data.eighth)/10000).toFixed(2);
						data.ninth = (parseFloat(data.ninth)/10000).toFixed(2);
						data.tenth = (parseFloat(data.tenth)/10000).toFixed(2);
						data.eleventh = (parseFloat(data.eleventh)/10000).toFixed(2);
						data.twelfth = (parseFloat(data.twelfth)/10000).toFixed(2);
					}
					else {
						data.first = parseFloat(data.first).toFixed(2);
						data.second = parseFloat(data.second).toFixed(2);
						data.third = parseFloat(data.third).toFixed(2);
						data.fourth = parseFloat(data.fourth).toFixed(2);
						data.fifth = parseFloat(data.fifth).toFixed(2);
						data.sixth = parseFloat(data.sixth).toFixed(2);
						data.seventh = parseFloat(data.seventh).toFixed(2);
						data.eighth = parseFloat(data.eighth).toFixed(2);
						data.ninth = parseFloat(data.ninth).toFixed(2);
						data.tenth = parseFloat(data.tenth).toFixed(2);
						data.eleventh = parseFloat(data.eleventh).toFixed(2);
						data.twelfth = parseFloat(data.twelfth).toFixed(2);
					}
					data.yoy = parseFloat(data.yoy).toFixed(2);
					data.mom = parseFloat(data.mom).toFixed(2);
				});
				return datas;
			}
//			SettleAnalyseDepartService.getDepartInfo(departId).
//				success(function(res){
//					if(res) {
//						$scope.$parent.query.doctor = doctor;
//						if(!$scope.$parent.departs || $scope.$parent.departs.length ===0) {
//							SettleAnalyseService.listAllDeparts(res.orgId).
//								success(function(res){
//									$scope.$parent.departs = res;
//								});
//						}
//					}
//				});

			$scope.$parent.query.depart = departId;
			
			if (!$scope.$parent.query.org) {
				$scope.$parent.query.org = orgCode;
			}
			
			$scope.settleAnalyseService = SettleAnalyseService;
			$scope.$watchCollection('settleAnalyseService.typeSelected',function(v){
				if (v) {
					getDeptTableBasicData();
				}
	    	});
			//获取统筹区 医疗机构，科室 数据
			function getDeptTableBasicData() {
				SettleAnalyseDepartService.getDeptTableBasicData($scope.$parent.query.indicator,departId,$scope.$parent.query.date,orgCode).
					success(function(res){
						if($scope.$parent.query.view  == 'org'){
							basicTableData = [];
							angular.forEach(res,function(data,index){
								if(data.type != 'region'){
									basicTableData.push(data);
								}
							});
						}
						else {
							basicTableData = res;
						}
						basicTableData = toFixedDatas(basicTableData);
						$scope.page.current = 1;
						query({
							start:1
						});
				});
			}
			
	}]);
	
})();